Conditions | 4 |
Total Lines | 58 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import { Inject } from '@nestjs/common'; |
||
33 | private async createNotification( |
||
34 | command: CreateNotificationCommand |
||
35 | ): Promise<string> { |
||
36 | const { message, type, leaveReaquest } = command; |
||
37 | |||
38 | if (type === NotificationType.POST) { |
||
39 | const { id } = await this.mattermostNotifier.createPost( |
||
40 | MATTERMOST_CHANNEL_LEAVES_ID, |
||
41 | message |
||
42 | ); |
||
43 | const notification = await this.notificationRepository.save( |
||
44 | new Notification(type, message, id, leaveReaquest) |
||
45 | ); |
||
46 | |||
47 | return notification.getId(); |
||
48 | } |
||
49 | |||
50 | if (type === NotificationType.REACTION) { |
||
51 | const rootNotification = await this.getRootNotification( |
||
52 | leaveReaquest.getId() |
||
53 | ); |
||
54 | |||
55 | await this.mattermostNotifier.createReaction( |
||
56 | rootNotification.getResourceId(), |
||
57 | message |
||
58 | ); |
||
59 | |||
60 | const notification = await this.notificationRepository.save( |
||
61 | new Notification( |
||
62 | type, |
||
63 | message, |
||
64 | rootNotification.getResourceId(), |
||
65 | leaveReaquest |
||
66 | ) |
||
67 | ); |
||
68 | |||
69 | return notification.getId(); |
||
70 | } |
||
71 | |||
72 | if (type === NotificationType.COMMENT) { |
||
73 | const rootNotification = await this.getRootNotification( |
||
74 | leaveReaquest.getId() |
||
75 | ); |
||
76 | |||
77 | const { id } = await this.mattermostNotifier.createComment( |
||
78 | this.configService.get<string>('MATTERMOST_CHANNEL_LEAVES_ID'), |
||
79 | message, |
||
80 | rootNotification.getResourceId() |
||
81 | ); |
||
82 | |||
83 | const notification = await this.notificationRepository.save( |
||
84 | new Notification(type, message, id, leaveReaquest) |
||
85 | ); |
||
86 | |||
87 | return notification.getId(); |
||
88 | } |
||
89 | |||
90 | throw new Error('Type not managed'); |
||
91 | } |
||
100 |